class Node:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next 

def solve(headA,headB):
    cura,curb=headA,headB
    while(cura!=curb):
        if cura:cura=cura.next
        else: cura=headB
        if curb:curb=curb.next
        else: curb=headA
    return cura 

def build(lsta,lstb,skipa,skipb):
    headA,headB=Node(lsta[0]),Node(lstb[0])
    cura,curb=headA,headB 
    for i in range(1,skipa):
        cura.next=Node(lsta[i])
        cura=cura.next 
    for i in range(1,skipb):
        curb.next=Node(lstb[i])
        curb=curb.next 
    headC=Node(lsta[skipa])
    curc=headC 
    for i in range(skipa+1,len(lsta)):
        curc.next=Node(lsta[i])
        curc=curc.next 
    cura.next=headC
    curb.next=headC 
    return headA,headB

headA,headB=build([4,1,8,4,5], [5,0,1,8,4,5],2,3)
print(solve(headA,headB).val)
        





        